package com.letoken.platform.repo.app.manager;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.letoken.platform.repo.app.dao.AppUserMapper;
import com.letoken.platform.repo.app.dao.AppUserTeamMapper;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.app.po.AppUserTeam;
import com.letoken.platform.repo.app.po.AppUserWallet;
import com.letoken.platform.repo.app.redis.AppLoginTokenRedis;
import com.letoken.platform.repo.utlis.WebUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * @version 1.0
 * @author： Christopher
 * @date： 2022/1/6 15:10
 */
@Log4j2
@Repository
public class AppUserManager {

    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private AppUserTeamMapper appUserTeamMapper;

    @Autowired
    private AppUserWalletMapper appUserWalletMapper;


    /**
     * 根据钱包地址查询用户信息
     *
     * @param walletAddres
     * @return
     */
    public AppUser queryByWalletAddres(String walletAddres) {
        QueryWrapper<AppUser> query = new QueryWrapper<>();
        query.eq("wallet_address", walletAddres);
        return appUserMapper.selectOne(query);
    }

    public AppUserWallet queryUseWallet(Integer userId, Integer tokenId) {
        QueryWrapper<AppUserWallet> query = new QueryWrapper<>();
        query.eq("user_id", userId).eq("token_id", tokenId);
        return appUserWalletMapper.selectOne(query);
    }

    /**
     * 添加用户
     *
     * @param addUser
     * @return
     */
    public Integer addUser(AppUser addUser) {
        return appUserMapper.insert(addUser);
    }


    /**
     * 更新用户信息
     *
     * @param appUser
     * @return
     */
    public Integer updateById(AppUser appUser) {
        return appUserMapper.updateById(appUser);
    }

    /**
     * 根据id查询用户信息
     *
     * @param userId
     * @return
     */
    public AppUser queryById(Integer userId) {
        return appUserMapper.selectById(userId);
    }


    public AppUser queryMe() {
        AppUser user = AppLoginTokenRedis.me().getClassObject(WebUtils.getToken());
//        if (ObjectUtil.isNull(user)) {
//            user = this.queryById(WebUtils.getUserId());
//            //更新用户缓存
//            AppLoginTokenRedis.me().put(WebUtils.getToken(), user);
//        }
        return user;
    }

    /**
     * 根据邀请码查询用户信息
     *
     * @param invitationCode
     * @return
     */
    public AppUser queryInvitationUser(String invitationCode) {
        QueryWrapper<AppUser> query = new QueryWrapper<>();
        query.eq("invitation_code", invitationCode);
        return appUserMapper.selectOne(query);
    }

    public AppUserTeam queryLevelByUserId(Integer userId) {
        LambdaQueryWrapper<AppUserTeam> teamWrapper = Wrappers.lambdaQuery();
        teamWrapper.eq(AppUserTeam::getUserId, userId);
        return appUserTeamMapper.selectOne(teamWrapper);
    }

//    public AppUser queryUserByToken(String token) {
//        LambdaQueryWrapper<AppUser> queryWrapper = Wrappers.lambdaQuery();
//        queryWrapper.eq(AppUser::getToken, token);
//        return appUserMapper.selectOne(queryWrapper);
//    }

    /**
     * 判断用户是否存在
     *
     * @param userId
     * @return
     */
    public boolean queryUserExistById(Integer userId) {
        LambdaQueryWrapper<AppUser> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUser::getId, userId);
        return appUserMapper.selectCount(queryWrapper) > 0;
    }
}
